package org.myapp.page.login.form;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.StringValidator;

import org.myapp.page.validator.AlphaNurmericValidator;
import org.myapp.service.ServiceLocator;
import org.myapp.service.login.ILoginService;

/**
 * フォームの基底クラス.<br>
 *
 * @author Administrator
 *
 * @param <T>
 */
public class BaseForm<T> extends Form<T> {

    private static final long serialVersionUID = 5906341609617844917L;

    private final TextField<String> userId = new TextField<String>("txtUserId");
    private final PasswordTextField password = new PasswordTextField("txtPassword1");
    private final FeedbackPanel message = new FeedbackPanel("message");
    private LoginFormModel model = null;

    private ILoginService service = ServiceLocator.instance().getService(ILoginService.class);
    protected ILoginService getLoginService() { return service; }

    /**
     * コンストラクタ.<br>
     * 各フォーム内に共通するコンポーネントを配置.<br>
     *
     * @param id フォームID
     */
    protected BaseForm(String id, LoginFormModel model) {
        super(id);

        setModel(new CompoundPropertyModel<T>(model));

        this.model = model;

        // require
        userId.setRequired(true);

        // minLength
        userId.add(StringValidator.minimumLength(6));
        password.add(StringValidator.minimumLength(6));

        // Pattern
        userId.add(new AlphaNurmericValidator());
        password.add(new AlphaNurmericValidator());

        add(userId, password, message);
    }

    public LoginFormModel getFormModel() {
        return this.model;
    }

    public TextField<String> getUserId() {
        return userId;
    }

    public PasswordTextField getPassword() {
        return password;
    }

    public FeedbackPanel getFeedBackPanel() {
    	return message;
    }
}
